package com.love_weinuo.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.love_weinuo.R;
import com.love_weinuo.databinding.WidgetMainTableViewBinding;
import com.love_weinuo.util.listener.OnLonglyListener;

/**
 * Created by Administrator
 * on 2019/2/21.
 */

public class MainTableView extends LinearLayout {
    private WidgetMainTableViewBinding binding;
    private OnTableChangedListener OnTableChangedListener;
    private int lastIndex = 0;
    private ImageView imgs[] = new ImageView[3];
    private int defaultImgRes[] = new int[3];
    private int selectImgRes[] = new int[3];
    public OnLonglyListener listener;

    public void setOnLonelyListener(OnLonglyListener listener) {
        this.listener = listener;
    }

    public MainTableView(Context context) {
        this(context, null);
    }

    public MainTableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.widget_main_table_view, this, true);
        binding.setPresenter(this);
        imgs[0] = binding.tableMessageIV;
        imgs[1] = binding.tableContactsIV;
        imgs[2] = binding.tableMyselfIV;
        defaultImgRes[0] = R.drawable.icon_first_select_before;
        selectImgRes[0] = R.drawable.icon_first_select_after;
        defaultImgRes[1] = R.drawable.icon_add_selelct_before;
        selectImgRes[1] = R.drawable.icon_add_select_after;
        defaultImgRes[2] = R.drawable.icon_my_select_before;
        selectImgRes[2] = R.drawable.icon_my_select_after;
        binding.rvModuleCar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onLonelyListener(0);
                }
            }
        });
    }

    public void onClick(View view) {
        int clickAt = Integer.parseInt(view.getTag() + "");
        onClick(clickAt);
    }

    /**
     * 手动触发点击事件
     */
    public void onClick(int index) {
        if (lastIndex == index) {
            return;
        }
        //更改底部点击图标的方法
        imgs[lastIndex].setImageResource(defaultImgRes[lastIndex]);
        imgs[index].setImageResource(selectImgRes[index]);
        lastIndex = index;
        if (OnTableChangedListener != null) {
            OnTableChangedListener.onTableChanged(index);
        }
    }

    public void setOnTableChangedListener(OnTableChangedListener onTableChangedListener) {
        this.OnTableChangedListener = onTableChangedListener;
    }

    public interface OnTableChangedListener {
        void onTableChanged(int index);
    }

    @SuppressLint("SetTextI18n")
    public void changeNumber(int number) {
        if (0 == number) {
            binding.textNumber.setVisibility(GONE);
        }
        binding.textNumber.setText(number + "");
    }
}

